package com.boilerCloud.dao.sys.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.sys.Dictionary;
import com.boilerCloud.dto.sys.DictionaryTypeInfo;
import com.boilerCloud.util.StringUtil;
@Repository
public class DictionaryDao extends BaseDao {

	public void qryList(Dictionary model, HzlqswPage<Dictionary> page)throws Exception {

		String countsql = "select count(*) from dictionary a "
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.* "
					+ " from dictionary a "
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getName())){
			countsql += " and a.name like ?";
			listSql += " and a.name like ?";
			params.add("%" + model.getName() +"%");
		}
		if(model.getType() != 0 && model.getType()!= -1){
			countsql += " and a.type= ?";
			listSql += " and  a.type= ?";
			params.add(model.getType() );
		}
		if ( !StringUtil.isEmpty(model.getCode()) ) {
			countsql += " and a.code= ?";
			listSql += " and  a.code= ?";
			params.add(model.getCode());
		}
		if ( !StringUtil.isEmpty(model.getParentId()) ) {
			countsql += " and a.parent_id= ?";
			listSql += " and  a.parent_id= ?";
			params.add(model.getParentId());
		}
		if (!StringUtil.isEmpty(model.getErtypeId())) {
			countsql += " and a.parent_id= ?";
			listSql += " and  a.parent_id= ?";
			params.add(model.getErtypeId());
		}
			
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, Dictionary.class);
	}

	public void save(Dictionary model) {
		String sql = "insert into dictionary(type,name,code,keyword,remark,attachment,is_delete,parent_id,ertype_id) "
				+ "values(?,?,?,?,?,?,'n',?,?)";
		List<Object> param = new ArrayList<Object>();
		param.add(model.getType());
		param.add(model.getName());
		param.add(model.getCode());
		param.add(model.getKeyword());
		param.add(model.getRemark());
		param.add(model.getAttachment());
		param.add(model.getParentId());
		param.add(model.getErtypeId());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}

	public void edit(Dictionary model) {
		List<Object> param = new ArrayList<Object>();
		String sql = "update dictionary set id= ? ";
		param.add(model.getId());
		
		if(!StringUtil.isEmpty(model.getName())){
			sql+= " , name=? ";
			param.add(model.getName());
		}
		if(!StringUtil.isEmpty(model.getCode())){
			sql+= " , code=? ";
			param.add(model.getCode());
		}

		if(!StringUtil.isEmpty(model.getType())){
			sql+= " , type=? ";
			param.add(model.getType());
		}

		if(!StringUtil.isEmpty(model.getKeyword())){
			sql+= " , keyword=? ";
			param.add(model.getKeyword());
		}

		if(!StringUtil.isEmpty(model.getRemark())){
			sql+= " , remark=? ";
			param.add(model.getRemark());
		}
		if ( !StringUtil.isEmpty(model.getParentId()) ) {
			sql += " , Parent_Id=? ";
			param.add(model.getParentId());
		}
		if (!StringUtil.isEmpty(model.getErtypeId())) {
			sql += " , ertype_Id=? ";
			param.add(model.getErtypeId());
		}
		if(!StringUtil.isEmpty(model.getAttachment())){
			sql+= " , attachment=? ";
			param.add(model.getAttachment());
		}
		sql+=" where id = ? ";
		param.add(model.getId());
		
		this.jdbcTemplate.update(sql,param.toArray());
	}

	public void del(int id) {
		String sql = "update dictionary set is_delete='y' where id=?";
		this.jdbcTemplate.update(sql,id);
		
	}

	public List<Dictionary> getDictionaryList(int type) throws Exception {
		String sql = "select * from dictionary where is_delete='n' and type=?";
		return this.queryForList(sql, Dictionary.class,type);
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 		根据id查询字典信息
	 *
	 * </blockquote></em></b>
	 * 
	 * @Title: qryDictionaryById
	 * @param id
	 * @return    
	 * @author Ran_cc
	 * @date 2018年5月24日 
	 */
	public Dictionary qryDictionaryById(int id) throws Exception {
		String sql = "select a.* ,b.id bid,b.name bname , c.id cid  , c.name cname  from dictionary a  left join dictionary b on a.parent_id = b.id  left join dictionary c on b.parent_id = c.id where a.is_delete='n' and a.id=?";
		return this.queryForObject(sql, Dictionary.class,id);
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 		根据id查询字典信息
	 *
	 * </blockquote></em></b>
	 * 
	 * @Title: qryDictionaryByCode
	 * @param code
	 * @return    
	 * @author Ran_cc
	 * @date 2018年5月24日 
	 */
	public Dictionary qryDictionaryByCode( Dictionary model ) throws Exception {
		String sql = "select * from dictionary a where a.is_delete='n' ";

		List<Object> params = new ArrayList<Object>();
		if ( !StringUtil.isEmpty(model.getCode()) ) {
			sql += " and  a.code= ?";
			params.add(model.getCode());
		}

		sql += " limit 1";

		return this.queryForObject(sql, Dictionary.class, params);
	}

	/**
	 * <b>Description:<em><blockquote>
	 * 根据id查询字典信息
	 * </blockquote></em></b>
	 * 
	 * @Title: qryDictionaryByCode
	 * @param code
	 * @return
	 * @author Ran_cc
	 * @date 2018年5月24日
	 */
	public Dictionary qryDictionaryByTypeInfo( Dictionary model ) throws Exception {
		String sql = "select * from dictionary a where a.is_delete='n' ";

		List<Object> params = new ArrayList<Object>();
		if ( !StringUtil.isEmpty(model.getCode()) ) {
			sql += " and  a.code= ?";
			params.add(model.getCode());
		}

		sql += " limit 1";

		return this.queryForObject(sql, Dictionary.class, params);
	}

	/**
	 * <b>Description:<em><blockquote>
	 * </blockquote></em></b>
	 * 
	 * @Title: getDictionary2Type
	 * @param dicModel
	 * @return
	 * @author Ran_cc
	 * @date 2018年6月3日
	 */
	public List<DictionaryTypeInfo> getDictionary2Type( DictionaryTypeInfo dicModel ) {
		String sql = "select * from dictionary a where a.is_delete='n' ";
		return null;
	}
}
